package com.chenu.gamscriptman.controller;

import com.chenu.gamscriptman.compoent.Global;
import com.chenu.gamscriptman.domain.GameData;
import com.chenu.gamscriptman.domain.noindb.GameMap;
import com.chenu.gamscriptman.domain.noindb.PageBean;
import com.chenu.gamscriptman.domain.noindb.Statistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author flyan
 * date 2020-01-02
 * @function
 */
@Controller
@ApiIgnore
public class GameDataController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisTemplate jsonRedisTemplate;

    /**
     * 游戏管理/列出所有用户
     */
    @GetMapping("/gamedatas")
    public String gamedatas(
            Model model
    ){
        // 放入分页数据和查询参数
        PageBean<GameData> gamedata_page = (PageBean<GameData>) jsonRedisTemplate.opsForValue().get(Global.REDIS_GAMEDATA_PAGES);
        model.addAttribute(Global.REDIS_GAMEDATA_PAGES, gamedata_page);
        GameData search_gamedata = (GameData) jsonRedisTemplate.opsForValue().get(Global.REDIS_SEARCH_GAMEDATA);
        model.addAttribute(Global.REDIS_SEARCH_GAMEDATA, search_gamedata);
        /* 放入统计信息 */
        Statistics gamedata_statistics = (Statistics) jsonRedisTemplate.opsForValue().get(Global.REDIS_GAMEDATA_STATISTICS);
        model.addAttribute(Global.REDIS_GAMEDATA_STATISTICS, gamedata_statistics);
        /* 所有的游戏名称 */
        model.addAttribute(Global.REDIS_SEARCH_GAME_NAME_LIST,
                (List<String>)jsonRedisTemplate.opsForValue().get(Global.REDIS_SEARCH_GAME_NAME_LIST));
        /* 很重要的一步，放入游戏配置映射 */
        model.addAttribute(Global.REDIS_GAMEDATA_MAP,
                (GameMap)jsonRedisTemplate.opsForValue().get(Global.REDIS_GAMEDATA_MAP));

        return "gamedata/list";
    }

    /**
     * 前往添加/编辑页面
     * @param model
     * @return
     */
    @GetMapping("/gamedata")
    public String gamedataAddOrEdit(
            Model model
    ){
        /* 放入编辑的用户 */
        GameData editGameData = (GameData) jsonRedisTemplate.opsForValue().get(Global.REDIS_EDIT_GAMEDATA);
        model.addAttribute(Global.REDIS_EDIT_GAMEDATA, editGameData);
        /* 很重要的一步，放入游戏配置映射 */
        model.addAttribute(Global.REDIS_GAMEDATA_MAP,
                (GameMap)jsonRedisTemplate.opsForValue().get(Global.REDIS_GAMEDATA_MAP));
        /* 同上，但是这次放的是游戏配置映射的json字符串，js处理很需要它 */
        model.addAttribute(Global.REDIS_GAMEDATA_MAP_JSON,
                (String)jsonRedisTemplate.opsForValue().get(Global.REDIS_GAMEDATA_MAP_JSON));


        return "gamedata/addOrEdit";
    }

}
